package pers.cz.job;

import pers.cz.cache.BaseDataCache;
import pers.cz.json.JsonUtil;
import pers.cz.mapper.AutoCollectTestCaseMapper;
import pers.cz.model.AutoCollectTestCaseWithBLOBs;
import pers.cz.quartz.job.AbstractQuartzJob;
import pers.cz.http.HttpUtils;
import pers.cz.spring.SpringContextUtil;
import pers.cz.thread.ThreadExecutor;
import pers.cz.thread.ThreadKeyFunction;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.testng.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试案例清洗(删除一下无效的测试案例)
 * @program: PostGirl-panent
 * @description: TestCaseClearingJob
 * @author: Cheng Zhi
 * @create: 2022-03-18 11:24
 **/
public class TestCaseClearingJob extends AbstractQuartzJob {

    private static final String DELETEALL = "0";   // 两张表都删除
    private static final String DELETEONCE = "1";  // 只删除接口出入参录制案例表
    @Override
    public void doJob(JobExecutionContext var1, Map params) throws JobExecutionException {

        /**
         * 案例清洗，兜取每个未打标记的录制案例，然后自动调用一次，如果通过了则添加到真正的案例表中，
         * 如果失败了，则将相关traceId的都删除。🚀🚀🚀
         */
        List<AutoCollectTestCaseWithBLOBs> list = SpringContextUtil.getBean(AutoCollectTestCaseMapper.class).selectAllCollectTestCase();

        for (AutoCollectTestCaseWithBLOBs autoCollectTestCase : list) {

            dealCollectTestCase(autoCollectTestCase);
        }
    }

    /**
     * 处理数据
     * @param autoCollectTestCase
     */
    private void dealCollectTestCase(AutoCollectTestCaseWithBLOBs autoCollectTestCase) {

        String request = autoCollectTestCase.getRequest();
        String response = autoCollectTestCase.getResponse();

        // 修改请求中的参数
        JsonElement jsonRequest = new JsonParser().parse(request);
        Map map = new HashMap();
        map.put("so_mode","999");
        String newRequest = JsonUtil.replaceJsonNode(jsonRequest, map).toString();

        // autoCollectTestCase.getExt1() 获取到的是录制的源头环境标识
        String url = SpringContextUtil.getBean(BaseDataCache.class).getHostsAddress().get(autoCollectTestCase.getExt1()).getHostAddress();
        // 发送请求
        String result = HttpUtils.getResultByPost(newRequest, url);
        // 这里是否要处理一下出参结果。

        String traceId = autoCollectTestCase.getExt2();
        if (!myAssert(response, result)) {
            // 对比不通过，需要删除数据 ,先获取到traceId, autoCollectTestCase.getExt2()中保存的就是traceId
            // 异步删除
            deleteAutoTestCase(traceId,DELETEALL);
        } else {
            deleteAutoTestCase(traceId,DELETEONCE);
        }

    }

    /**
     * 异步删除录制案例和录制方法
     * @param traceId
     */
    private void deleteAutoTestCase(String traceId, String operType) {

        ThreadExecutor execute = ThreadExecutor.getInstance(ThreadKeyFunction.INTERFACE_COLLECT.getIndex());
        execute.executeTask(new DeleteAutoTestCaseThread(traceId,"0"));
    }
    /**
     * 断言
     * @param response
     * @param result
     * @return
     */
    private boolean myAssert(String response, String result) {
        boolean flag = false;
        // 断言
        try {
            Assert.assertEquals(response,result);
            flag = true;
        } catch (AssertionError e) {
            flag = false;
        }
        return flag;
    }
}
